package com.ms.tank.game;

import com.ms.tank.abstractfactory.BaseTank;
import com.ms.tank.common.PropertyMgr;
import com.ms.tank.common.ResourceManager;
import com.ms.tank.enums.Dir;
import com.ms.tank.enums.Group;
import com.ms.tank.fire.FireStrategy;
import com.ms.tank.utils.Audio;

import java.awt.*;
import java.util.Random;

/**
 * 坦克类
 *
 * @Date 2021/3/9 20:06:19
 * @Author mashuai
 */
public class Tank extends BaseTank {
    public int x, y;
    public Dir dir = Dir.UP;
    //移动值
    private final int SPEED = Integer.parseInt(PropertyMgr.get("tankSpeed").toString());
    public TankFrame tankFrame;
    public static int WIDTH = ResourceManager.goodTankU.getWidth();
    public static int HEIGHT = ResourceManager.goodTankU.getHeight();
    //是否移动
    private boolean moving = true;
    //是否存活
    private boolean living = true;
    private Random random = new Random();
    public FireStrategy fs;


    public Tank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tankFrame = tankFrame;
        this.group = group;

        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.height = HEIGHT;
        rectangle.width = WIDTH;
        try {
            if (group == Group.GOOD) {
                String goodFSName = PropertyMgr.get("goodFS").toString();

                fs = (FireStrategy) Class.forName(goodFSName).getDeclaredConstructor().newInstance();

            } else {
                String badFSName = PropertyMgr.get("badFS").toString();

                fs = (FireStrategy) Class.forName(badFSName).getDeclaredConstructor().newInstance();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void paint(Graphics g) {
        //如果当前坦克不存活则移除坦克列表中的当前坦克对象
        if (!living) tankFrame.tanks.remove(this);
        //判断好坦克还是坏坦克加载

        //加载相应移动的坦克图片
        switch (dir) {
            case LEFT:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankL : ResourceManager.badTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankR : ResourceManager.badTankR, x, y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankU : ResourceManager.badTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankD : ResourceManager.badTankD, x, y, null);
                break;
        }
        //移动方法
        move();
    }


    private void move() {
        //判断是否移动状态
        if (!moving) return;

        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }

        rectangle.x = this.x;
        rectangle.y = this.y;

        //随机发火
        if (this.group == Group.BAD && random.nextInt(100) > 80) this.fire();
        //随机移动
        if (this.group == Group.BAD && random.nextInt(100) > 95) {
            randomDir();
        }
        //边界检测
        boundsCheck();
    }

    private void boundsCheck() {
        if (this.x < 2) x = 2;
        if (this.y < 28) y = 28;
        if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) x = TankFrame.GAME_WIDTH - Tank.WIDTH;
        if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
    }

    //随机移动
    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void fire() {
        fs.fire(this);
        //开火时调用子弹 子弹从中心发出
//        int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
//        int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
//
//        Dir[] dirs = Dir.values();
//        for(Dir dir : dirs) {
//            tankFrame.gameFactory.createBullet(bX, bY, dir, group, tankFrame);
//        }

        if(group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
    }

    //坦克死亡方法
    @Override
    public void die() {
        this.living = false;
    }



    public void setGroup(Group group) {
        this.group = group;
    }

    @Override
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    @Override
    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public int getSPEED() {
        return SPEED;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }


}
